package com.lwk.download2.utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileUtils extends Activity {
    /**
     获得挂载USB外部储存设备的目录
     */
    /*public String getUsbPath(){
        String newPath = null;
        File[] files;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            for(File file:files){
                Log.e("main", String.valueOf(file));
                if (!String.valueOf(file).substring(0,18).equals("/storage/emulated/"))
                    newPath=String.valueOf(file);
                System.out.println("判断之后："+newPath);
            }
        }
        return newPath;
    }*/
    /**
     * 复制文件夹及其中的文件
     *
     * @param oldPath String 原文件夹路径 如：data/user/0/com.test/files
     * @param newPath String 复制后的路径 如：data/user/0/com.test/cache
     * @return <code>true</code> if and only if the directory and files were copied;
     *         <code>false</code> otherwise
     */
    public static boolean copyFolder(String oldPath, String newPath, Context context) {
//        System.out.println("即将要创建的目录："+newPath);
        if (newPath == null){
            Toast.makeText(context, "U盘不存在！", Toast.LENGTH_SHORT).show();
            return false;
        }
        newPath+="/QR";
        try {
            File newFile = new File(newPath);
            if (!newFile.exists()) {
                if (!newFile.mkdirs()) {
                    Log.e("--Method--", "copyFolder: cannot create directory.");
                    return false;
                }
            }
            File oldFile = new File(oldPath);
//            System.out.println("路径是否存在："+oldFile.exists());
            if (!oldFile.exists()) {
                Toast.makeText(context, "所要复制的路径不存在！", Toast.LENGTH_SHORT).show();
                return false;
            }
//            System.out.println("输入参数："+oldPath);
            String[] files = oldFile.list();
//            System.out.println("路径是否存在："+files.length);+++++++++++++++++++++++++++++++++++++++++++++++++++
//            for (String file : files) {
//                System.out.println("判断："+file);
//            }
            File temp;
            for (String file : files) {
                if (file.length()> 4 && file.substring(file.lastIndexOf('.')).equals(".jpg")) {
                    System.out.println("文件名：" + file);
                    if (oldPath.endsWith(File.separator)) {
                        temp = new File(oldPath + file);
                    } else {
                        temp = new File(oldPath + File.separator + file);
                    }
                    if (temp.isDirectory()) {   //如果是子文件夹
                            copyFolder(oldPath + "/" + file, newPath + "/" + file,context);
                    } else if (!temp.exists()) {
                        Log.e("--Method--", "copyFolder:  oldFile not exist.");
                        return false;
                    } else if (!temp.isFile()) {
                        Log.e("--Method--", "copyFolder:  oldFile not file.");
                        return false;
                    } else if (!temp.canRead()) {
                        Log.e("--Method--", "copyFolder:  oldFile cannot read.");
                        return false;
                    } else {
                        FileInputStream fileInputStream = new FileInputStream(temp);
                        FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
                        System.out.println("得到的文件名：" + temp.getName());
                        byte[] buffer = new byte[1024];
                        int byteRead;
                        while ((byteRead = fileInputStream.read(buffer)) != -1) {
                            fileOutputStream.write(buffer, 0, byteRead);
                        }
                        fileInputStream.close();
                        fileOutputStream.flush();
                        fileOutputStream.close();
                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}